import { ConfigEnv, loadEnv, UserConfig } from "vite";
import path from "path";
import { setupVitePlugins } from "./build/plugin";
import { getServiceEnvConfig } from "./build/config/env-config";
import { createViteProxy } from "./build/config/proxy";

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const viteEnv = loadEnv(mode, process.cwd()) as unknown as ViteEnv;

  const isOpenProxy = viteEnv.VITE_HTTP_PROXY === "Y";
  const envConfig = getServiceEnvConfig(viteEnv);

  const isBuild = command === "build";
  return {
    base: viteEnv.VITE_BASE_URL,
    resolve: {
      alias: {
        "@": path.join(__dirname, "./src"),
      },
    },
    plugins: setupVitePlugins(viteEnv, isBuild),
    server: {
      host: true,
      port: 5173,
      proxy: createViteProxy(isOpenProxy, envConfig),
    },
  };
};
